package com.ph.irp.stock.action;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.ph.irp.dto.PageDTO;
import com.ph.irp.dto.StockInStorageDTO;
import com.ph.irp.dto.StockInStorageDetailDTO;
import com.ph.irp.model.PStockInStorage;
import com.ph.irp.model.PStockInStorageDetail;
import com.ph.irp.stock.service.StockInStorageService;
import com.ph.util.DateUtil;
import com.ph.util.pagination.PageUtil;
import com.ph.util.pagination.ShowPageList;

public class StockInStorageAction extends ActionSupport implements ShowPageList {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8842938049541234538L;
	
	private PageDTO page;
	private String cpage;

	private StockInStorageService stockInStorageService;

	private PStockInStorage stockInStorage;
	
	private StockInStorageDTO stockInStorageDTO;
	
	private List<StockInStorageDTO> stockInStorageDTOList;
	private List<StockInStorageDTO> tempStockInStorageDTOList;
	
	private String beginDate;
	
	private String endDate;
	
	private List<StockInStorageDetailDTO> stockInStorageDetailDTOList;
	
	/**
	 * 选择的详情中的Id
	 */
	private String[] rowlist;

	// 详情里面的响应列的信息

	private String[] materialId;

	private String[] storageId;

	private String[] materialNO;

	private String[] measureUnitId;

	private String[] shouldAmount;

	private String[] basicShouldAmount;

	private String[] factAmount;

	private String[] basicFactAmount;

	private String[] unitPrice;

	private String[] money;

	private String[] remark;

	private String[] inStorageDate;

	private String[] inStoreLife;

	private String[] validationDate;

	private String[] sourceBillCode;

	private String[] sourceOrderCode;
	

	public List<StockInStorageDTO> getTempStockInStorageDTOList() {
		return tempStockInStorageDTOList;
	}

	public void setTempStockInStorageDTOList(
			List<StockInStorageDTO> tempStockInStorageDTOList) {
		this.tempStockInStorageDTOList = tempStockInStorageDTOList;
	}

	public String[] getRowlist() {
		return rowlist;
	}

	public void setRowlist(String[] rowlist) {
		this.rowlist = rowlist;
	}

	public StockInStorageDTO getStockInStorageDTO() {
		return stockInStorageDTO;
	}

	public void setStockInStorageDTO(StockInStorageDTO stockInStorageDTO) {
		this.stockInStorageDTO = stockInStorageDTO;
	}

	public List<StockInStorageDetailDTO> getStockInStorageDetailDTOList() {
		return stockInStorageDetailDTOList;
	}

	public void setStockInStorageDetailDTOList(
			List<StockInStorageDetailDTO> stockInStorageDetailDTOList) {
		this.stockInStorageDetailDTOList = stockInStorageDetailDTOList;
	}

	public String getBeginDate() {
		return beginDate;
	}

	public void setBeginDate(String beginDate) {
		this.beginDate = beginDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public List<StockInStorageDTO> getStockInStorageDTOList() {
		return stockInStorageDTOList;
	}

	public void setStockInStorageDTOList(
			List<StockInStorageDTO> stockInStorageDTOList) {
		this.stockInStorageDTOList = stockInStorageDTOList;
	}

	public String[] getMaterialId() {
		return materialId;
	}

	public String[] getStorageId() {
		return storageId;
	}

	public void setStorageId(String[] storageId) {
		this.storageId = storageId;
	}

	public String[] getMaterialNO() {
		return materialNO;
	}

	public void setMaterialNO(String[] materialNO) {
		this.materialNO = materialNO;
	}

	public String[] getMeasureUnitId() {
		return measureUnitId;
	}

	public void setMeasureUnitId(String[] measureUnitId) {
		this.measureUnitId = measureUnitId;
	}

	public String[] getShouldAmount() {
		return shouldAmount;
	}

	public void setShouldAmount(String[] shouldAmount) {
		this.shouldAmount = shouldAmount;
	}

	public String[] getBasicShouldAmount() {
		return basicShouldAmount;
	}

	public void setBasicShouldAmount(String[] basicShouldAmount) {
		this.basicShouldAmount = basicShouldAmount;
	}

	public String[] getFactAmount() {
		return factAmount;
	}

	public void setFactAmount(String[] factAmount) {
		this.factAmount = factAmount;
	}

	public String[] getBasicFactAmount() {
		return basicFactAmount;
	}

	public void setBasicFactAmount(String[] basicFactAmount) {
		this.basicFactAmount = basicFactAmount;
	}

	public String[] getUnitPrice() {
		return unitPrice;
	}

	public void setUnitPrice(String[] unitPrice) {
		this.unitPrice = unitPrice;
	}

	public String[] getMoney() {
		return money;
	}

	public void setMoney(String[] money) {
		this.money = money;
	}

	public String[] getRemark() {
		return remark;
	}

	public String[] getInStorageDate() {
		return inStorageDate;
	}

	public void setInStorageDate(String[] inStorageDate) {
		this.inStorageDate = inStorageDate;
	}

	public String[] getInStoreLife() {
		return inStoreLife;
	}

	public void setInStoreLife(String[] inStoreLife) {
		this.inStoreLife = inStoreLife;
	}

	public String[] getValidationDate() {
		return validationDate;
	}

	public void setValidationDate(String[] validationDate) {
		this.validationDate = validationDate;
	}

	public void setRemark(String[] remark) {
		this.remark = remark;
	}

	public String[] getSourceBillCode() {
		return sourceBillCode;
	}

	public void setSourceBillCode(String[] sourceBillCode) {
		this.sourceBillCode = sourceBillCode;
	}

	public String[] getSourceOrderCode() {
		return sourceOrderCode;
	}

	public void setSourceOrderCode(String[] sourceOrderCode) {
		this.sourceOrderCode = sourceOrderCode;
	}

	public void setMaterialId(String[] materialId) {
		this.materialId = materialId;
	}

	public StockInStorageService getStockInStorageService() {
		return stockInStorageService;
	}

	public void setStockInStorageService(
			StockInStorageService stockInStorageService) {
		this.stockInStorageService = stockInStorageService;
	}

	public PStockInStorage getStockInStorage() {
		return stockInStorage;
	}

	public void setStockInStorage(PStockInStorage stockInStorage) {
		this.stockInStorage = stockInStorage;
	}

	public PageDTO getPage() {
		return page;
	}

	public void setPage(PageDTO page) {
		this.page = page;
	}

	public String getCpage() {
		return cpage;
	}

	public void setCpage(String cpage) {
		this.cpage = cpage;
	}

	public String addStockInStorageInput() {

		stockInStorage = new PStockInStorage();
		// 为新增采购订单设置默认的时间
		Date currentDate = new Date();
		String stringDate = DateUtil.date2String(currentDate, "yyyy-MM-dd");
		// 设置采购入库日期
		stockInStorage.setInStorageDate(stringDate);

		return "success";
	}

	public String addStockInStorage() throws Throwable {
		
		HttpSession session = ServletActionContext.getRequest().getSession();
		String username = (String)session.getAttribute("username");
		stockInStorage.setMakeBiller(username);
		stockInStorageService.addStockInStorage(stockInStorage);
		int stockInStorageId = stockInStorageService
				.getStockInStorageIdByCode(stockInStorage.getInStorageCode());
		PStockInStorageDetail stockInStorageDetail = new PStockInStorageDetail();
		for (int i = 0; i < materialId.length; i++) {
			stockInStorageDetail.setStockInStorageId(stockInStorageId);
			stockInStorageDetail.setMaterialId(Integer.parseInt(materialId[i]));
			stockInStorageDetail.setStorageId(Integer.parseInt(storageId[i]));
			stockInStorageDetail.setMaterialNo(materialNO[i]);
			stockInStorageDetail.setMeasureUnitId(Integer
					.parseInt(measureUnitId[i]));
			stockInStorageDetail.setShouldAmount(Float
					.parseFloat(shouldAmount[i]));
			stockInStorageDetail.setBasicUnitShouldAmount(Float
					.parseFloat(basicShouldAmount[i]));
			stockInStorageDetail.setFactAmount(Float.parseFloat(factAmount[i]));
			stockInStorageDetail.setBasicUnitFactAmount(Float
					.parseFloat(basicFactAmount[i]));
			stockInStorageDetail.setUnitPrice(Float.parseFloat(unitPrice[i]));
			stockInStorageDetail.setMoney(Float.parseFloat(money[i]));
			stockInStorageDetail.setRemark(remark[i]);

			stockInStorageDetail.setInStorageDate(inStorageDate[i]);
			if (inStoreLife[i] == null || inStoreLife[i].trim().equals(""))
				stockInStorageDetail.setInStoreLife(0f);
			else
				stockInStorageDetail.setInStoreLife(Float
						.parseFloat(inStoreLife[i]));
			stockInStorageDetail.setValidationDate(validationDate[i]);

			stockInStorageDetail.setSourceBillCode(sourceBillCode[i]);
			stockInStorageDetail.setSourceOrderCode(sourceOrderCode[i]);
			stockInStorageService.addStockInStorageDetail(stockInStorageDetail);
		}
		return "success";
	}
	
	public String deleteStockInStorage() throws Throwable {
		for(String stockInStorageId : rowlist)
			stockInStorageService.deleteStockInStorage(stockInStorageId);
		return "list";
	}
	
	@Override
	public String showPageList() throws Throwable {
		if(tempStockInStorageDTOList != null) {
			page = PageUtil.generatePageDTO(tempStockInStorageDTOList, "showStockInStoragePageList.action", cpage);
			stockInStorageDTOList = page.getResList();
			return "success";
		} else {
			return "input";
		}
	}

	public String showStockInStorage() throws Throwable {
		tempStockInStorageDTOList = stockInStorageService.getAllStockInStorages();
		page = PageUtil.generatePageDTO(tempStockInStorageDTOList, "showStockInStoragePageList.action", cpage);
		stockInStorageDTOList = page.getResList();
		return "list";
	}
	
	public String getStockInStorages() throws Throwable {
		tempStockInStorageDTOList = stockInStorageService.getStockInStorages(stockInStorage, beginDate, endDate);
		page = PageUtil.generatePageDTO(tempStockInStorageDTOList, "showStockInStoragePageList.action", cpage);
		stockInStorageDTOList = page.getResList();
		stockInStorage = null;
		return "list";
	}
	
	public String showStockInStorageDetails() throws Throwable {
		stockInStorageDetailDTOList = stockInStorageService.getStockInStorageDetailsByInStorageId(Integer.parseInt(rowlist[0]));
		stockInStorageDTO = stockInStorageService.getStockInStorageById(Integer.parseInt(rowlist[0]));
		return "list";
	}
	
}
